'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  /**
   *
   * @api {POST} /api/user/register 客户端-用户注册
   * @apiName 用户注册
   * @apiGroup 用户模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {String} phone 手机号
   * @apiParam  {String} name 昵称
   * @apiParam  {String} password 密码
   * @apiParam  {Number} projectId 项目id
   *
   * @apiParamExample  {type} Request-Example:
   *   {
   *       "phone":"13699999999",
   *       "name":"qwer",
   *       "password":"123456"
   *   }
   *
   *
   * @apiSuccessExample {type} Success-Response:
   {
      "code": 200,
      "msg": "成功操作",
      "data": {
          "avatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //用户头像
          "desc": "呢个人好懒，咩都冇留低",               //用户个人简介
          "blogCount": 0,                               //博客总数
          "followCount": 0,                             //关注作者总数
          "fanCount": 0,                                //粉丝总数
          "likeCount": 0,                               //点赞次数
          "historyCount": 0,                            //浏览历史次数
          "commentCount": 0,                            //评论次数
          "replyCount": 0,                              //被回复次数(我的回复)
          "status": 1,                                  //账号状态
          "id": 20,                                     //用户id
          "phone": "13699999999",                       //用户手机号
          "name": "qwer",                               //用户昵称
          "projectId": "4",                             //项目id
          "updatedAt": "2021-10-09T01:48:10.880Z",      //更新个人信息时间
          "createdAt": "2021-10-09T01:48:10.880Z"       //创建用户时间
      }
  }
   *
   *
   */
  // 注册
  async register() {
    const { app, ctx, service } = this;
    const data = ctx.request.body;
    const checkRes = new this.ctx.helper.checkData(
      this,
      'phone',
      'name',
      'password',
      'projectId'
    ).checkDataRes;
    try {
      if (checkRes) {
        // 加密密码
        data.password = await ctx.genHash(String(data.password));
        console.log('加密之后的密码: ', data.password);
        const res = await service.blog.user.register();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('注册失败');
    }
  }

  /**
   *
   * @api {POST} /api/user/login 客户端-用户登录
   * @apiName 用户登录
   * @apiGroup 用户模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {String} phone 手机号
   * @apiParam  {String} password 密码
   *
   * @apiSuccess (200) {type} name description
   *
   * @apiParamExample  {type} Request-Example:
      {
          "phone":"13699999999",
          "password":"123456"
      }
   *
   *
   * @apiSuccessExample {type} Success-Response:
  {
      "code": 200,
      "msg": "成功操作",
      "data": {
          "id": 16,                       //用户id
          "phone": "13622222222",         //用户手机号
          "name": "mojito",               //用户昵称
          "avatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //用户头像
          "desc": "呢个人好懒，咩都冇留低",     //用户个人简介
          "blogCount": 8,                     //博客总数
          "followCount": 0,                   //关注作者总数
          "fanCount": 0,                      //粉丝总数
          "likeCount": 1,                     //点赞次数
          "historyCount": 20,                 //浏览历史次数
          "replyCount": 0,                    //被回复次数(我的回复)
          "commentCount": 1,                  //评论次数
          "status": 1,                        //账号状态
          "projectId": "4",                    //项目id
          "createdAt": "2021-09-28T07:01:33.000Z",        //创建用户时间
          "updatedAt": "2021-10-09T01:41:17.000Z",        //更新个人信息时间
          "deletedAt": null,
          "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTYsImlhdCI6MTYzMzc0NDI5OCwiZXhwIjoxNjMzODMwNjk4fQ.RNVukYgGoIFrPupgJtCpNKsPxZU2k3NqlIxYUVA6olM",
          "expire_time": 1638510846000 //token过期时间
      }
  }
   *
   *
   */
  // 登录
  async login() {
    const { app, ctx, service } = this;

    const checkRes = new this.ctx.helper.checkData(this, 'phone', 'password')
      .checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.user.login();
        if (res == '账号不存在') {
          return ctx.helper.errorModule(res);
        }
        if (res == '账号已被封') {
          return ctx.helper.errorModule(res);
        }
        if (res == '用户密码错误') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('登录失败');
    }
  }
  /**
   *
   * @api {GET} /api/user/searchUser?id=28&projectId=4 客户端-查看用户信息(需要token)
   * @apiName 查看用户信息
   * @apiGroup 用户模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} id 用户id(用户查询自己则有点赞记录和历史记录)
   *
   *
   * @apiSuccessExample {type} Success-Response:
  {
      "code": 200,
      "msg": "成功操作",
      "data": {
          "id": 17,                           //用户id
          "phone": "13622222223",             //用户手机号
          "name": "mojito",                   //用户昵称
          "avatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //用户头像
          "desc": "呢个人好懒，咩都冇留低",         //用户个人简介
          "blogCount": 0,                         //博客总数
          "followCount": 0,                       //关注作者总数
          "fanCount": 0,                          //粉丝总数
          "commentCount": 0,                      //评论次数
          "replyCount": 0,                        //被回复次数(我的回复)
          "status": 1,                            //账号状态
          "projectId": "4",                           //项目id
          "createdAt": "2021-09-28T07:02:27.000Z",    //创建用户时间
          "updatedAt": "2021-10-08T02:44:07.000Z",    //更新个人信息时间
          "deletedAt": null
      }
  }
   *
   *
   */
  // 获取用户信息(如果获取自己信息则显示likeCount histiryCount)
  async searchUser() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'id').checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.user.searchUser();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
   *
   * @api {POST} /api/user/follow  客户端-关注/取消关注用户(需要token)
   * @apiName 关注/取消关注用户
   * @apiGroup 用户模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} id 用户id
   * @apiParam  {Number} projectId 项目id
   *
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "关注成功"/"取消关注成功",
          "data": []
      }
   *
   *
   */
  // 关注用户
  async follow() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'id', 'projectId')
      .checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.user.follow();
        if (res == '用户不存在') {
          return ctx.helper.errorModule(res);
        }
        // res==1说明本来已关注
        if (res == '取消关注成功') {
          return ctx.helper.successModule('取消关注成功');
        }
        return ctx.helper.successModule('关注成功');
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
   *
   * @api {PUT} /api/user/resetPassword 客户端-重置密码(用户需要token)
   * @apiName 重置密码
   * @apiGroup 用户模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {String} oldPassword 旧密码
   * @apiParam  {String} newPassword 新密码
   *
   *
   * @apiParamExample  {type} Request-Example:
      {
          "oldPassword":"123456",
          "newPassword":"1234567"
      }
   *
   *
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "修改成功",
          "data": []
      }
   *
   *
   */
  // 重置密码
  async resetPassword() {
    const { app, ctx, service } = this;

    const checkRes = new ctx.helper.checkData(
      this,
      'oldPassword',
      'newPassword'
    ).checkDataRes;

    try {
      if (checkRes) {
        const res = await service.blog.user.resetPassword();
        if (res == '密码验证失败') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule('修改成功');
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('修改失败');
    }
  }

  // 编辑个人信息
  async edit() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'name', 'avatar', 'desc')
      .checkDataRes;

    try {
      if (checkRes) {
        await service.blog.user.edit();
        return ctx.helper.successModule('修改成功');
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('修改失败');
    }
  }

  /**
   *
   * @api {POST} /api/user/isRegister 客户端-判断手机号是否已注册
   * @apiName 判断手机号是否已注册
   * @apiGroup 用户模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {String} phone 手机号
   *
   * @apiParamExample  {type} Request-Example:
      {
          "phone":"13622222992"
      }
   *
   *
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "手机号未注册",
          "data": []
      }
   *
   *
   */
  // 判断是否已注册
  async isRegister() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'phone').checkDataRes;
    // 如果有缺少参数
    try {
      if (checkRes) {
        const res = await service.blog.user.isRegister();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      return ctx.helper.errorModule(error);
    }
  }
  /**
   *
   * @api {GET} /api/user/getLikeBlog 客户端-获取点赞过的博客(需要token)
   * @apiName 获取点赞过的博客
   * @apiGroup 用户模块
   * @apiVersion  1.0.0
   *
   *
   *
   *
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "成功操作",
          "data": {
              "count": 2,         //点赞博客总数
              "rows": [
                  {
                      "id": 13,           //点赞记录id
                      "blogId": 175,      //博客id
                      "likeUserId": 40,   //点赞该博客的用户的id
                      "projectId": 11,    //项目id
                      "createdAt": "2021-10-18T03:51:15.000Z",    //点赞时间
                      "updatedAt": "2021-10-18T03:51:15.000Z",
                      "deletedAt": null,
                      "blog": {
                          "id": 178,              //博客id
                          "userId": 40,           //博客作者id
                          "userName": "blog",     //博客作者昵称
                          "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png", //博客作者头像
                          "projectId": 11,        //项目id
                          "title": "blog",        //博客标题
                          "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",    //博客封面图
                          "content": "blog",      //博客内容
                          "read_count": 5,        //博客浏览次数
                          "like_count": 1,        //博客点赞次数
                          "comment_count": 0,     //博客评论次数
                          "createdAt": "2021-10-18T02:08:50.000Z",
                          "updatedAt": "2021-10-18T03:51:15.000Z",
                          "deletedAt": null
                      },
                      "BlogClassifies": [
                      {
                          "classifyId": 35,       //博客分类id
                          "classifyName": "html"  //博客分类名
                      },
                      {
                          "classifyId": 37,
                          "classifyName": "js"
                      }
                  ]
                  },
                  {
                      "id": 14,
                      "blogId": 178,
                      "likeUserId": 40,
                      "projectId": 11,
                      "createdAt": "2021-10-18T03:51:40.000Z",
                      "updatedAt": "2021-10-18T03:51:40.000Z",
                      "deletedAt": null,
                      "blog": {
                          "id": 178,
                          "userId": 40,
                          "userName": "blog",
                          "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                          "projectId": 11,
                          "title": "blog2",
                          "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                          "content": "blog2",
                          "read_count": 1,
                          "like_count": 1,
                          "comment_count": 0,
                          "createdAt": "2021-10-18T02:54:06.000Z",
                          "updatedAt": "2021-10-18T03:51:41.000Z",
                          "deletedAt": null
                      }
                  },
                  "BlogClassifies": [
                      {
                          "classifyId": 35,
                          "classifyName": "html"
                      },
                      {
                          "classifyId": 37,
                          "classifyName": "js"
                      }
                  ]
              ]
          }
      }
   *
   *
   */
  // 获取点赞的博客
  async getLikeBlog() {
    const { app, ctx, service } = this;

    try {
      const res = await service.blog.user.getLikeBlog();
      return ctx.helper.successModule(res);
    } catch (error) {
      return ctx.helper.errorModule(error);
    }
  }

  /**
   *
   * @api {GET} /api/user/getLikeUser 客户端-获取关注的用户(需要token)
   * @apiName 获取关注的用户
   * @apiGroup 用户模块
   * @apiVersion  1.0.0
   *
   *
   *
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "成功操作",
          "data": {
              "count": 2,             //关注的人的总数
              "rows": [
                  {
                      "id": 32,               //关注记录id
                      "followUserId": 40,     //关注的人的id
                      "beFollowUserId": 29,   //被关注的人的id
                      "projectId": 4,         //项目id
                      "createdAt": "2021-10-20T07:28:20.000Z",    //关注时间
                      "updatedAt": "2021-10-20T07:28:20.000Z",
                      "deletedAt": null,
                       "user": {
                          "phone": "13622222222",     //被关注的用户的手机号
                          "name": "mojito",           //被关注的人的昵称
                          "avatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",    //被关注的人的头像
                          "desc": "呢个人好懒，咩都冇留低"         //被关注的人的个人简介
                      }
                  },
              ]
          }
      }
   *
   *
   */
  // 获取关注的用户
  async getLikeUser() {
    const { app, ctx, service } = this;
    const res = await service.blog.user.getLikeUser();
    return ctx.helper.successModule(res);
  }
}
module.exports = UserController;
